//
//  JKRShareManager.m
//  BaoJiDianJing
//
//  Created by Lucky on 2017/8/25.
//  Copyright © 2017年 KaiHei. All rights reserved.
//

#import "JKRShareManager.h"
#import "JKRSharePlatformController.h"
#import "UIImage+JKRShareManagerCategory.h"

@implementation JKRShareManager

#pragma mark - 分享URL并弹出分享平台选择面板
+ (void)jkr_shareURLWithTitle:(NSString *)title icon:(id)icon subHead:(NSString *)subHead url:(NSString *)url currentViewController:(id)currentViewController block:(shareManagerBlock)block {
    [JKRSharePlatformController showShareMenuInCurrentController:currentViewController platformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [JKRShareManager jkr_shareURLWithTitle:title icon:icon subHead:subHead url:url shareToPlatform:platformType currentViewController:currentViewController block:^(BOOL success, UMSocialPlatformType platformType) {
            if(block) block(success, platformType);
        }];
    }];
}

#pragma mark - 分享URL到指定平台
+ (void)jkr_shareURLWithTitle:(NSString *)title icon:(id)icon subHead:(NSString *)subHead url:(NSString *)url shareToPlatform:(UMSocialPlatformType)platformType currentViewController:(id)currentViewController block:(shareManagerBlock)block {
    if (!currentViewController) {
        currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:subHead thumImage:icon];
    shareObject.webpageUrl = url;
    messageObject.shareObject = shareObject;
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentViewController completion:^(id result, NSError *error) {
        if (!error) {
            if(block) block(YES, platformType);
        } else {
            if(block) block(NO, platformType);
        }
    }];
}

#pragma mark - 分享图片并弹出分享平台选择面板
+ (void)jkr_shareImageWithImage:(UIImage *)image currentViewController:(id)currentViewController block:(shareManagerBlock)block{
    [JKRSharePlatformController showShareMenuInCurrentController:currentViewController platformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [JKRShareManager jkr_shareImageWithImage:image shareToPlatform:platformType currentViewController:currentViewController block:^(BOOL success, UMSocialPlatformType platformType) {
            if (block) block(success, platformType);
        }];
    }];
}

#pragma mark - 分享图片到指定平台
+ (void)jkr_shareImageWithImage:(UIImage *)image shareToPlatform:(UMSocialPlatformType)platformType currentViewController:(id)currentViewController block:(shareManagerBlock)block{
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    shareObject.shareImage = image;
    messageObject.shareObject = shareObject;
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
        if (!error) {
            if(block) block(YES, platformType);
        } else {
            if(block) block(NO, platformType);
        }
    }];
}

#pragma mark - 分享小程序
+ (void)jkr_shareMiniProgramWithTitle:(NSString *)title desc:(NSString *)desc thumImage:(id)thumImage webpageUrl:(NSString *)webpageUrl userName:(NSString *)userName path:(NSString *)path currentViewController:(id)currentViewController block:(shareManagerBlock)block {
    if (!currentViewController) {
        currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:title descr:desc thumImage:thumImage];
    shareObject.webpageUrl = webpageUrl;
    shareObject.userName = userName;
    shareObject.path = path;
#ifdef DEBUG
    shareObject.miniProgramType = UShareWXMiniProgramTypePreview;
#else
    shareObject.miniProgramType = UShareWXMiniProgramTypeRelease;
#endif
    messageObject.shareObject = shareObject;
    if ([thumImage isKindOfClass:[UIImage class]]) {
        [thumImage jkr_share_manager_compressToDataLength:128 * 1024 withBlock:^(NSData *data) {
            shareObject.hdImageData = data;
            [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:currentViewController completion:^(id result, NSError *error) {
                if (!error) {
                    if(block) block(YES, UMSocialPlatformType_WechatSession);
                } else {
                    if(block) block(NO, UMSocialPlatformType_WechatSession);
                }
            }];
        }];
    } else {
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:currentViewController completion:^(id result, NSError *error) {
            if (!error) {
                if(block) block(YES, UMSocialPlatformType_WechatSession);
            } else {
                if(block) block(NO, UMSocialPlatformType_WechatSession);
            }
        }];
    }

}

@end
